from lxml import etree

html_tree = etree.parse('070_xpath_html.html')

# li后面的文本 北京 上海 深圳 武汉
# li_list = html_tree.xpath('//body/ul/li/text()')

# 查找所有有id的属性的li标签
# text()获取标签中的内容
# li_list = html_tree.xpath('//body/ul/li[@id]/text()')

# 找到id为l1的li标签  注意引号的问题
# li_list = html_tree.xpath('//body/ul/li[@id="c4"]/text()')

# 查找到id为l1的li标签的class的属性值
# li_list = html_tree.xpath('//body/ul/li[@id="l1"]/@class')

# 查询id中包含l的li标签
# li_list = html_tree.xpath('//body/ul/li[starts-with(@id,"1")]/text()')

# 查询id为l1且class为c1的 and 与 | 效果一样
# 查询id为l1或class为c1的 or
# li_list = html_tree.xpath('//body/ul/li[@id="l1" and @class="c1"]/text()')
li_list = html_tree.xpath('//body/ul/li[@id="l1"]/text() | //body/ul/li[@class="c1"]/text()')

# 查询id的值以l开头的li标签

for item in li_list:
    print(item)
